<?php

class Core_Model_Website_Collection_Block extends Dcmp_Model_Collection_Strong {

    public function __construct() {
        parent::__construct();
        $this->setItemClass('website/block');
        $this->from('{t}website_block', 'a')
                ->field('a.*')
                ->field('b.title', 'module_type')
                ->field('b.name', 'module_name')
                ->innerJoin('{t}global_module', 'b', 'a.module_id', 'b.id');
    }

    public function filterBy($name, $arg) {
        switch ($name) {
            case 'id':
                $this->addParameter('id', $arg)->eq('a.id', ':id:i');
                break;
            case 'code':
                $this->addParameter('code', $arg)->eq('a.code', ':code:s');
                break;
            case 'name':
                $this->addParameter('name', "%$arg%")->like('a.name', ':name:s');
                break;
            case 'area';
                $this->addParameter('areaId', $arg)->eq('a.area_id', ':areaId:i');
                break;
            case 'enabled':
                $this->addParameter('enabled', $arg)->eq('a.enabled', ':enabled:i');
                break;
            case 'moduleid':
                $this->addParameter('moduleId', $arg)->eq('a.module_id', ':moduleId:i');
                break;
            case 'moduleid':
                $this->addParameter('moduleId', $arg)->eq('a.module_id', ':moduleId:i');
                break;
        }
        return parent::filterBy($name, $arg);
    }

    public function joinAccount() {
        $this->join()->inner('{t}permission_account', 'd')->on('a.creator_id', 'd.id')
                ->closest()
                ->select()->field('d.full_name', 'creator');
        return $this;
    }
}

